package cn.iclass.modules.sys.controller;

import cn.iclass.common.annotation.SysLog;
import cn.iclass.common.constant.CommonConstant;
import cn.iclass.common.exception.GlobalException;
import cn.iclass.common.utils.Result;
import cn.iclass.common.utils.ShiroUtils;
import cn.iclass.modules.sys.entity.SysMenuEntity;
import cn.iclass.modules.sys.service.ShiroService;
import cn.iclass.modules.sys.service.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 系统菜单
 *
 * @author alan
 * @date 2016年10月27日 下午9:58:15
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private ShiroService shiroService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/nav")
	public Result nav() {
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(ShiroUtils.getUserId());
		Set<String> permissions = shiroService.getUserPermissions(ShiroUtils.getUserId());
		Map<String, Object> result = new HashMap<>(2);
		result.put("menuList", menuList);
		result.put("permissions", permissions);
		return Result.ok(result);
	}

	/**
	 * 所有菜单列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public List<SysMenuEntity> list() {
		List<SysMenuEntity> menuList = sysMenuService.list(null);
		for (SysMenuEntity sysMenuEntity : menuList) {
			SysMenuEntity parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
			if (parentMenuEntity != null) {
				sysMenuEntity.setParentName(parentMenuEntity.getName());
			}
		}

		return menuList;
	}

	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public Result<List<SysMenuEntity>> select() {
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();

		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);

		return Result.ok(menuList);
	}

	/**
	 * 菜单信息
	 */
	@GetMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public Result<SysMenuEntity> info(@PathVariable("menuId") Long menuId) {
		SysMenuEntity menu = sysMenuService.getById(menuId);
		return Result.ok(menu);
	}

	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@PostMapping("/save")
	@RequiresPermissions("sys:menu:save")
	public Result save(@RequestBody SysMenuEntity menu) {
		//数据校验
		verifyForm(menu);

		sysMenuService.save(menu);

		return Result.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@PostMapping("/update")
	@RequiresPermissions("sys:menu:update")
	public Result update(@RequestBody SysMenuEntity menu) {
		//数据校验
		verifyForm(menu);

		sysMenuService.updateById(menu);

		return Result.ok();
	}

	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@PostMapping("/delete/{menuId}")
	@RequiresPermissions("sys:menu:delete")
	public Result delete(@PathVariable("menuId") long menuId) {
		if (menuId <= 31) {
			return Result.error("系统菜单，不能删除");
		}

		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if (menuList.size() > 0) {
			return Result.error("请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return Result.ok();
	}

	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu) {
		if (StringUtils.isBlank(menu.getName())) {
			throw new GlobalException("菜单名称不能为空");
		}

		if (menu.getParentId() == null) {
			throw new GlobalException("上级菜单不能为空");
		}

		//菜单
		if (menu.getType() == CommonConstant.MenuType.MENU.getValue()) {
			if (StringUtils.isBlank(menu.getUrl())) {
				throw new GlobalException("菜单URL不能为空");
			}
		}

		//上级菜单类型
		int parentType = CommonConstant.MenuType.CATALOG.getValue();
		if (menu.getParentId() != 0) {
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}

		//目录、菜单
		if (menu.getType() == CommonConstant.MenuType.CATALOG.getValue() ||
				menu.getType() == CommonConstant.MenuType.MENU.getValue()) {
			if (parentType != CommonConstant.MenuType.CATALOG.getValue()) {
				throw new GlobalException("上级菜单只能为目录类型");
			}
			return;
		}

		//按钮
		if (menu.getType() == CommonConstant.MenuType.BUTTON.getValue()) {
			if (parentType != CommonConstant.MenuType.MENU.getValue()) {
				throw new GlobalException("上级菜单只能为菜单类型");
			}
			return;
		}
	}
}
